<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>活动管理</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../../styles/common.css"/>
    <link rel="stylesheet" href="../../../styles/page.css"/>
    <link rel="stylesheet" href="../../../styles/bttn.min.css"/>

    <style>
        #activity-app .notAdmin::after {
            border: 0 !important;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="activity-app">
    <div class="container">
        <div class="tableBar">
            <el-input v-model="input"
                      placeholder="请输入活动名称"
                      style="width: 250px"
                      clearable
                      @keyup.enter.native="handleQuery"
            >
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery"
                ></i>
            </el-input>
            <el-button
                    type="primary"
                    @click="addActivityHandle('add')"
                    class="bttn-pill bttn-md bttn-default"
            >
                + 添加活动
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                :header-cell-style="{background:'#eef1f6',color:'#606266',fontSize:'18px'}"
        >
            <el-table-column
                    prop="activityTitle"
                    label="活动名称"
            >
                <template slot-scope="scope">
                    <div style="font-size: 18px">
                        {{ scope.row.activityTitle }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                    prop="activityBtime"
                    label="活动开始时间"
            >
                <template slot-scope="scope">
                    <div style="font-size: 18px">
                        {{ scope.row.activityBtime }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                    prop="activityEtime"
                    label="活动结束时间"
            >
                <template slot-scope="scope">
                    <div style="font-size: 18px">
                        {{ scope.row.activityEtime }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                    prop="activityAddress"
                    label="活动地点"
            >
                <template slot-scope="scope">
                    <div style="font-size: 18px">
                        {{ scope.row.activityAddress }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="活动状态">
                <template slot-scope="scope">
                    <div style="font-size: 18px">
                    {{ String(scope.row.activityStatus) === '0' ? '已结束' : '进行中' }}
                    </div>
                </template>

            </el-table-column>
            <el-table-column
                    label="操作"
                    width="160"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="medium"
                            class="blueBug"
                            @click="addActivityHandle(scope.row.id)"
                            :class="{notAdmin:user !== 'admin'}"
                    >
                        <div style="font-size: 15px">
                           编辑
                        </div>
                    </el-button>
                    <el-button
                            type="text"
                            size="medium"
                            class="delBut non"
                            @click="deleteActivityHandle(scope.row.id)"
                    >
                        <div style="font-size: 15px">
                            删除
                        </div>
                    </el-button>
                    <el-button
                            type="text"
                            size="medium"
                            class="delBut non"
                            @click="statusHandle(scope.row)"
                            v-if="user === 'admin' || user === 'superAdmin'"
                    >
                        <div style="font-size: 15px">
                        {{ scope.row.activityStatus == '1' ? '结束活动' : '开启活动' }}
                        </div>
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../../plugins/axios/axios.min.js"></script>
<script src="../../../js/request.js"></script>
<script src="../../../api/activity.js"></script>
<script>
    new Vue({
        el: '#activity-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData: [],
                id: '',
                activityStatus: '',
                userId: '',
                user: ''
            }
        },
        computed: {},
        created() {
            if (window.localStorage.getItem('adminInfo') === null) {
                window.location.href = '../login/loginBackend.html'
            }
            console.log(JSON.parse(localStorage.getItem('adminInfo')))
            this.user = JSON.parse(localStorage.getItem('adminInfo')).volunteerPermission
            this.userId = JSON.parse(localStorage.getItem('adminInfo')).id
            this.init()
        },
        mounted() {
        },
        methods: {
            async init() {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined,
                    handlerId: this.userId
                }
                await getActivityList(params).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加
            addActivityHandle(st) {
                if (st === 'add') {
                    window.parent.menuHandle({
                        id: '2',
                        url: 'activity/add.html',
                        name: '添加活动'
                    }, true)
                } else {
                    window.parent.menuHandle({
                        id: '2',
                        url: 'activity/add.html?id=' + st,
                        name: '编辑活动'
                    }, true)
                }
            },
            //删除
            deleteActivityHandle(st) {
                this.$confirm('此操作将删除活动, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteActivity(st).then(res => {
                        if (String(res.code) === '1') {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.init();
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //状态修改
            statusHandle(row) {
                this.id = row.id
                this.activityStatus = row.activityStatus
                this.$confirm('确认调整该活动的状态?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    enableOrDisableActivity({
                        id: this.id,
                        activityStatus: !this.activityStatus ? 1 : 0
                    }).then(res => {
                        if (String(res.code) === '1') {
                            this.$message.success('活动状态更改成功！')
                            this.handleQuery()
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
        }
    })
</script>
</body>
</html>